package explorer;

import gui.graphicals.RightClickPopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


/**
 * 
 * @author Carmen
 */

@SuppressWarnings("serial")
public class RightClickPopupMenuExplorer extends RightClickPopupMenu implements ActionListener {
    
    private JComponent _parent = null;
    
    /* Variables to copy/cut */
    private String _action = "";
    private File _fileClipboard = null;
    private JCheckBox hiddenFileCheckBox = new JCheckBox("Afficher les fichiers cachés");
    private boolean _showHidden;


    public RightClickPopupMenuExplorer() {
        
        addItemToMenu("Renommer", "edit.png", new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae){
                System.out.println("On clique sur Renommer");
                setVisible(false);
            }
        });
        
        addItemToMenu("Supprimer", "delete.png", this);
        
        addSeparator();
        
        addItemToMenu("Couper", "cut.png", this);
        addItemToMenu("Copier", "copy.png", this);
        addItemToMenu("Coller", "paste.png", this);
        
        addSeparator();
        
        hiddenFileCheckBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (hiddenFileCheckBox.isSelected()) {
                    _showHidden = true;
                } else {
                    _showHidden = false;
                }
            }
            
        });
        add(hiddenFileCheckBox);
    }
    
    public void cutFile(File src, File dest) throws IOException {
        System.out.println("On coupe " + src.getAbsolutePath() + " dans " + dest.getAbsolutePath());
        
        if (src.isFile()) {
            FileTools.move(src.getAbsolutePath(), dest.getAbsolutePath());
        } else if (src.isDirectory()) {
            
            File newCopiedDirectory = new File(dest.getAbsolutePath() + "/" + src.getName());
            newCopiedDirectory.mkdir();
                
            FileTools.copyDirectory(src, newCopiedDirectory);
            
            FileTools.deleteDirectory(new File(src.getAbsolutePath()));
        }
        
    }
    
    public void copyFile(File src, File dest) throws IOException {
        if (!dest.isDirectory()) {
            return;
        }
        System.out.println("On copie " + src.getAbsolutePath() + " dans " + dest.getAbsolutePath());
        if (src.isFile()) {
            FileTools.copy(src.getAbsolutePath(), dest.getAbsolutePath());
        } else if (src.isDirectory()) {
            
            File newCopiedDirectory = new File(dest.getAbsolutePath() + "/" + src.getName());
            newCopiedDirectory.mkdir();
            
            FileTools.copyDirectory(src, newCopiedDirectory);
        }
    }
    
    public void setSource(Object source) {
        _parent = (JComponent) source;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        
        setVisible(false);
        
        File tmp = null;
        
        try {
            if (_parent instanceof JTree) {
                tmp = new File(((FileModel) ((JTree) _parent).getLastSelectedPathComponent()).getPath());
            } else if (_parent instanceof JTable) {
                _parent = (JTable) _parent;
                tmp = new File(((FileTableModel) ((JTable)_parent).getModel()).getValueAt(((JTable) _parent).getSelectedRow()).getPath());
            } else {
                return;
            }
        } catch (NullPointerException ex) {
            System.err.println("Nothing is selected... Sorry !");
        }
        
        if (ae.getActionCommand().equalsIgnoreCase("COUPER")) {
            _action = "CUT";
            _fileClipboard = tmp;
            
        } else if (ae.getActionCommand().equalsIgnoreCase("COPIER")) {
            _action = "COPY";
            _fileClipboard = tmp;
            
        } else if (ae.getActionCommand().equalsIgnoreCase("COLLER") && _fileClipboard != null) {
            
            if (_action.equals("CUT")) {
                try {
                    cutFile(_fileClipboard, tmp);
                } catch (IOException ex) {
                    Logger.getLogger(RightClickPopupMenuExplorer.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            } else if (_action.equals("COPY")) {
                try {
                    copyFile(_fileClipboard, tmp);
                } catch (IOException ex) {
                    Logger.getLogger(RightClickPopupMenuExplorer.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }
            
        } else if (ae.getActionCommand().equalsIgnoreCase("SUPPRIMER")) {
            try {
                delete(tmp);
            } catch (IOException ex) {
                Logger.getLogger(RightClickPopupMenuExplorer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void delete(File toDelete) throws IOException {
            if (toDelete.isFile()) {
                FileTools.removeFile(toDelete.getAbsolutePath(), "/");
            } else {
                FileTools.deleteDirectory(new File(toDelete.getAbsolutePath()));
            }
    }
    
    /**
     * @return the _showHidden
     */
    public boolean showHidden() {
        return _showHidden;
    }
 
}

